package com.guochenglang.www.controller;

import com.guochenglang.www.pojo.Base;
import com.guochenglang.www.service.BaseService;
import com.guochenglang.www.service.SearchService;
import com.guochenglang.www.util.GetObject;
import com.guochenglang.www.util.MyDependency;
import com.guochenglang.www.web.result.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

/**
 * 知识库Controller层
 *
 * @author 23217
 */

public class BaseController {
    private static final BaseService BASE_SERVICE = MyDependency.returnObject(BaseService.class);
    private static final SearchService SEARCH_SERVICE = MyDependency.returnObject(SearchService.class);

    /**
     * 获取用户知识库
     */
    public static Result showBase(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        int id = GetObject.getObject(request, Integer.class);
        ArrayList<Base> bases = null;
        //返回知识库表
        bases = BASE_SERVICE.myControlBase(id);

        return Result.result(bases);
    }

    /**
     * 添加知识库
     */
    public static Result addBase(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, InterruptedException, IllegalAccessException {
        //获得知识库数据
        Base base = GetObject.getObject(request, Base.class);
        //生成创建时间
        base.setCreatTime(new Timestamp(System.currentTimeMillis()));
        //添加知识库
        int key = BASE_SERVICE.createBase(base);
        return Result.result(key);
    }

    /**
     * 搜索知识库
     */
    public static Result searchBase(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取搜索信息
        String keyword = request.getParameter("input");
        keyword = new String(keyword.getBytes("ISO8859-1"), "utf-8");
        System.out.println(keyword);
        //搜索知识库

        ArrayList<Base> bases = SEARCH_SERVICE.baseSearch(keyword);
        System.out.println(bases);
        return Result.result(bases);
    }

    /**
     * 删除知识库
     */
    public static Result deleteBase(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, InterruptedException, IllegalAccessException {
        //获得知识库id
        int id = GetObject.getObject(request, Integer.class);
        //删除知识库
        int num = BASE_SERVICE.deleteBase(id);
        return Result.result(num);
    }

    /**
     * 更新知识库
     */
    public static Result updateBase(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得更新的知识库数据
        Base base = GetObject.getObject(request, Base.class);
        //获得知识库id
        int id = base.getId();
        Base baseCondition = new Base();
        baseCondition.setId(id);
        //更新数据
        int num = BASE_SERVICE.changeBase(base, baseCondition);
        return Result.result(num);
    }

    /**
     * 加入的知识库列表
     */
    public static Result myEnterBase(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        int userId = Integer.parseInt(request.getParameter("userId"));
        //搜索加入的知识库
        ArrayList<Base> bases = BASE_SERVICE.myEnterBase(userId);
        System.out.println(bases);
        return Result.result(bases);

    }
}
